package puj.promobil.prometer;

import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class BarCodeActivity  extends Activity {
		
		 
	     private Button bot1;
	     private Button bot2;
	     private Button bot3;
	     private TextView tv1;
	     
	   
	 	
	 	 private ImageView mImageView;
	 	private Bitmap mImageBitmap;

	    @Override
	    public void onCreate(Bundle savedInstanceState) 
	    {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_main);
	     
	        bot1=(Button) findViewById(R.id.botoncito);
	        bot2=(Button) findViewById(R.id.botoncito2);
	        bot3=(Button) findViewById(R.id.botonfoto);
	        tv1=(TextView)findViewById(R.id.textico);
	        mImageView = (ImageView) findViewById(R.id.imageView1);
	        mImageBitmap=null;
	        bot1.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	        	 Intent intent = new Intent("com.google.zxing.client.android.SCAN");
	             intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
	             startActivityForResult(intent, 0);
	             }
	        });
	        
	        bot2.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	        	 Intent intent = new Intent("com.google.zxing.client.android.SCAN");
	             intent.putExtra("SCAN_MODE", "BARCODE_MODE");
	             startActivityForResult(intent, 0);
	             }
	        });
	        
	        
	        bot3.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	            	dispatchTakePictureIntent();
	             }
	        });
	        
	    }
	    
	   
	    
	    
	    
	    
	    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
	        if (requestCode == 0) {
	            if (resultCode == RESULT_OK) {
	                String contents = intent.getStringExtra("SCAN_RESULT");
	               // String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
	                tv1.setText(contents);
	                // Handle successful scan
	            }
	        }
	            else if(requestCode == 1)
	            {
	            	if (resultCode == RESULT_OK) {
	            		tv1.setText("bien tomada");
	    				handleSmallCameraPhoto(intent);
	    			}
	            	
	            }
	            else if (resultCode == RESULT_CANCELED) {
	                // Handle cancel
	            }
	           
	        
	    }
	    
	    

	    @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	        getMenuInflater().inflate(R.menu.activity_main, menu);
	        return true;
	    }
	    
	    private void dispatchTakePictureIntent() {

	    	  Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
	    	  

			startActivityForResult(takePictureIntent, 1);
		}
	    
	    
	    
	    
	    private void handleSmallCameraPhoto(Intent intent) {
	        Bundle extras = intent.getExtras();
	        mImageBitmap = (Bitmap) extras.get("data");
	        mImageView.setImageBitmap(mImageBitmap);
	    }
	    
	   
	}

